package com.imau.brand.controller;

import com.alibaba.fastjson.JSON;
import com.imau.brand.bean.Brand;
import com.imau.brand.bean.PageEntitry;
import com.imau.brand.service.BrandService;
import com.imau.brand.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/brandAll")
public class BrandAllController extends HttpServlet {

    /**初始化业务层对象*/
    private BrandService brandService = new BrandServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("BrandAllController...");

        //1.获取前端分页参数
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //2.获取查询条件

        String jsonQuery = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonQuery, Brand.class);
        //3.调用业务层方法
        PageEntitry<Brand> pageEntitry = brandService.selectPageByCondition(Integer.valueOf(currentPage), Integer.valueOf(pageSize),brand);
        //2.转成JSON
        String jsonString = JSON.toJSONString(pageEntitry);
        //3.响应客户端
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
